package com.fmdj.customer.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.customer.controller.form.CustomerCarEntity;
import com.fmdj.customer.controller.form.DeleteCustomerCarByIdForm;
import com.fmdj.customer.controller.form.InsertCustomerCarForm;
import com.fmdj.customer.controller.form.SelectCustomerCarListForm;
import com.fmdj.customer.service.CustomerCarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/customer/car")
@Tag(name = "CustomerController", description = "客户车辆Web接口")
public class CustomerCarController {

    @Autowired
    private CustomerCarService customerCarService;

    @PostMapping("/insertCustomerCar")
    @Operation(summary = "添加车辆")
    @SaCheckLogin
    public CommonResult insertCustomerCar(@Valid @RequestBody InsertCustomerCarForm form) {
        CustomerCarEntity customerCarEntity = new CustomerCarEntity();
        long customerId = StpUtil.getLoginIdAsLong();
        customerCarEntity.setCustomerId(customerId);
//swagger-ui测试可以先不登陆 不用SaCheckLogin取数据 自己给数据测试 customerCarEntity.setCustomerId(1134423170456465400);
        customerCarEntity.setCarPlate(form.getCarPlate());
        customerCarEntity.setCarType(form.getCarType());
        int rows = customerCarService.insert(customerCarEntity);
        if (rows != 0) {
            return CommonResult.ok();
        }
        return CommonResult.error("添加车辆时出错");
    }

    @PostMapping("/searchCustomerCarList")
    @Operation(summary = "查询客户车辆列表")
    @SaCheckLogin
    public CommonResult selectCustomerCar() {
        SelectCustomerCarListForm form = new SelectCustomerCarListForm();
        form.setCustomerId(StpUtil.getLoginIdAsLong());

        ArrayList<HashMap> result = customerCarService.selectCarList(form);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/deleteCustomerCarById")
    @Operation(summary = "删除车辆信息")
    public CommonResult deleteCustomerCarById(@Valid @RequestBody DeleteCustomerCarByIdForm form) {
        int rows = customerCarService.deleteCustomerCarById(form);
        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }

}
